<template>
  <el-container direction="vertical" style="padding: 10px;">
    <el-form :inline="true" size="mini">
      <el-form-item label="组织名">
        <el-input v-model="searchForm.orgName"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="searchForm.ssn"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="searchForm.phoneNum"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="searchForm.roleId" filterable placeholder="选择角色">
          <el-option
            v-for="item in this.roleList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">清空</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      style="width: 100%"
      size="mini">
      <el-table-column
        prop="id"
        label="ID"
        min-width="5%">
      </el-table-column>
      <el-table-column
        prop="ssn"
        label="用户名"
        min-width="10%">
      </el-table-column>
      <el-table-column
        prop="username"
        label="账号"
        min-width="10%">
      </el-table-column>
      <el-table-column
        prop="phoneNum"
        label="联系方式"
        min-width="10%">
      </el-table-column>
      <el-table-column
        prop="roleName"
        label="角色"
        min-width="10%">
      </el-table-column>
      <el-table-column
        prop="orgName"
        label="组织"
        min-width="20%">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        min-width="20%">
        <template slot-scope="scope">
          <el-button @click="detail(scope.row)" type="text" size="small">查看</el-button>
          <el-button @click="update(scope.row)" type="text" size="small">编辑</el-button>
          <el-button type="text" @click="deleteUser(scope.row)" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="padding: 10px 10px 5px 10px;"
      :current-page="pageData.current"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="pageData.size"
      layout="total, prev, pager, next, jumper"
      :total="total"
    />
  </el-container>
</template>

<script>
  export default {
    name: 'UserPage',

    data: function() {
      return {
        roleList: [],
        dataList: [],
        searchForm: {
          orgName: null,
          ssn: null,
          phoneNum: null,
          roleId: null,
        },
        pageData: {
          current: 1,
          size: 10,
        },
        total: 0,
      }
    },
    created() {
      this.getRoleList();
      this.search();
    },
    methods: {
      search() {
        let formData = {...this.searchForm};
        formData['current'] = this.pageData.current;
        formData['size'] = this.pageData.size;
        this.$api.userPage(formData).then(res => {
          console.log(res)
          this.dataList = res.records;
          this.total = res.total;
          this.pageData.current = res.current;
          this.pageData.size = res.size;
        })
      },
      getRoleList() {
        this.$api.roleList().then(res => {
          this.roleList = res;
        })
      },
      handleSizeChange(val) {
        this.pageData.size = val;
        this.search();
      },
      handleCurrentChange(val) {
        this.pageData.current = val;
        this.search();
      },
      reset() {
        Object.assign(this.$data.searchForm, this.$options.data().searchForm)
      },
      deleteUser(row) {
        this.$confirm(`确定删除[${row.ssn}:${row.roleName}]吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.userDelete(row.id).then(res => {
            this.search();
          })
        })
      },
      detail(row) {
        this.$router.push({name:"UserAdd", params:{userId: row.id, update: false, readOnly:true}});
      },
      update(row) {
        this.$router.push({name:"UserAdd", params:{userId: row.id, update: true, readOnly:false}});
      }
    }
  }
</script>

<style scoped>

</style>
